package com.fenqing.leetcode.code.t300_400;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fenqing
 */
public class Topic387 extends Topic {

    /**
     * 构造器
     * @param input        输入
     */
    public Topic387(String input) {
        super(input);
    }

    @Execute
    public int firstUniqChar(String s) {
        Map<Character, Integer> hash = new HashMap<>();
        char[] chars = s.toCharArray();
        for (char aChar : chars) {
            Integer num = hash.getOrDefault(aChar, 0);
            hash.put(aChar, num + 1);
        }
        for (int i = 0; i < chars.length; i++) {
            if (hash.get(chars[i]) == 1) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        Topic387 topic387 = new Topic387("""
                loveleetcode
                """);
        int run = topic387.run();
        System.out.println(run);
    }

}
